package com.ujiuye.service;

import com.github.pagehelper.PageHelper;
import com.ujiuye.mapper.DataCollectMapper;
import com.ujiuye.pojo.DataCollect;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
@Service
public class DataCollectServiceImpl implements DataCollectService {
    @Autowired
    private DataCollectMapper dataCollectMapper;

    @Override
    public List<DataCollect> list() {
        return dataCollectMapper.list();
    }

    @Override
    public DataCollect getById(Integer id) {
        return dataCollectMapper.getById(id);
    }

    @Override
    public int save(DataCollect dataCollect) {
        return dataCollectMapper.save(dataCollect);
    }

    @Override
    public int update(DataCollect dataCollect) {
        return dataCollectMapper.update(dataCollect);
    }

    @Override
    public int remove(Integer id) {
        return dataCollectMapper.remove(id);
    }

    @Override
    public int removeAll(Integer[] ids) {
        return dataCollectMapper.removeAll(ids);
    }

    @Override
    public File getDownloadFile(int pageNum) {
        PageHelper.startPage(pageNum,3);
        List<DataCollect> list = dataCollectMapper.list();
        File file = new File("");
        String path = file.getAbsolutePath() + "/src/main/webapp/upload/";
        File downloadFile = new File(path+"对标信息管理第"+pageNum+"页数据.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        XSSFSheet sheet = workbook.createSheet("第一页");
        XSSFRow sheetRow = sheet.createRow(0);
        sheetRow.createCell(0).setCellValue("目标公司名称");
        sheetRow.createCell(1).setCellValue("年营业额");
        sheetRow.createCell(2).setCellValue("年度");
        sheetRow.createCell(3).setCellValue("企业性质");
        sheetRow.createCell(4).setCellValue("优势");
        sheetRow.createCell(5).setCellValue("劣势");
        sheetRow.createCell(6).setCellValue("行业排名");
        sheetRow.createCell(7).setCellValue("员工数量");
        sheetRow.createCell(8).setCellValue("企业创建时间");
        sheetRow.createCell(9).setCellValue("简单描述");
        for (int i=0;i<list.size();i++){
            XSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(list.get(i).getCname());
            row.createCell(1).setCellValue(list.get(i).getTurnover());
            row.createCell(2).setCellValue(sdf.format(list.get(i).getTime()));
            row.createCell(3).setCellValue(list.get(i).getBusiness());
            row.createCell(4).setCellValue(list.get(i).getSuperiority());
            row.createCell(5).setCellValue(list.get(i).getInforiority());
            row.createCell(6).setCellValue(list.get(i).getSort());
            row.createCell(7).setCellValue(list.get(i).getEmployeecount());
            row.createCell(8).setCellValue(sdf.format(list.get(i).getBuildtime()));
            row.createCell(9).setCellValue(list.get(i).getRemark());
        }
        try {
            workbook.write(new FileOutputStream(downloadFile));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return downloadFile;
    }
}
